<?php
require_once("db/db_connector.php");
require_once("db/db_paradise.php");
require_once("db/db_picture.php");

$keyword = NULL;
$order_by = NULL;
$order = NULL;

if (isset($_GET["keyword"])) {
	$keyword = $_GET["keyword"];
}

if (isset($_GET["order_by"])) {
	$order_by = $_GET["order_by"];
}

if (isset($_GET["order"])) {
	$order = $_GET["order"];
}

// Database Access Begins ---------------------------------------- //
$con = get_db_con();
$paradise_list = get_paradise_list($con, $keyword, $order_by, $order);
close_db_con();
// Database Access Ends ------------------------------------------ //
?>

<?php
include_once("include/header.php");
?>

<h2>Paradise Over the World</h2>

<div>
	<form action="index.php" method="get">
		<input type="hidden" name="order_by" value="<?=$order_by?>" />
		<input type="hidden" name="order" value="<?=$order?>" />
		<input type="text" name="keyword" value="<?=$keyword?>" />
		<button type="submit">Search</button>
	</form>
</div>

<div>
	<span>Sort By : </span>
	Name(<a href="index.php?keyword=<?=$keyword?>&order_by=name&order=ASC">ASC</a> |
		<a href="index.php?keyword=<?=$keyword?>&order_by=name&order=DESC">DESC</a>) |
	Climate Low(<a href="index.php?keyword=<?=$keyword?>&order_by=climate_low&order=ASC">ASC</a> |
		<a href="index.php?keyword=<?=$keyword?>&order_by=climate_low&order=DESC">DESC</a>) |
	Climate High(<a href="index.php?keyword=<?=$keyword?>&order_by=climate_high&order=ASC">ASC</a> |
		<a href="index.php?keyword=<?=$keyword?>&order_by=climate_high&order=DESC">DESC</a>) |
	Poluation(<a href="index.php?keyword=<?=$keyword?>&order_by=population&order=ASC">ASC</a> |
		<a href="index.php?keyword=<?=$keyword?>&order_by=population&order=DESC">DESC</a>) |
	GDP(<a href="index.php?keyword=<?=$keyword?>&order_by=gdp&order=ASC">ASC</a> |
		<a href="index.php?keyword=<?=$keyword?>&order_by=gdp&order=DESC">DESC</a>)
</div>

<hr />

<table>
	<tr>
		<th>thumb_small</th>
		<th>name</th>
		<th>rating</th>
		<th>type</th>
		<th>climate_high</th>
		<th>climate_low</th>
		<th>continent</th>
		<th>language</th>
		<th>religion</th>
		<th>population</th>
		<th>gdp</th>
	</tr>
<?php foreach ($paradise_list as $paradise): ?>
	<tr>
		<td>
        <?php
        if(is_null($paradise["picture_id"]))
            echo "NO IMAGE";
        else{
            $con = get_db_con();
            $image = get_picture($con, $paradise["picture_id"]);
            echo "<img src=\"" . $image["thumb_small"]. "\" />";
            close_db_con();
        }
        ?>
		</td>
		<td>
			<a href="paradise.php?id=<?=$paradise["id"]?>">
				<?=$paradise["name"]?>
			</a>
		</td>
		<td><?=$paradise["rating"]?></td>
		<td><?=$paradise["type"]?></td>
		<td><?=$paradise["climate_high"]?></td>
		<td><?=$paradise["climate_low"]?></td>
		<td><?=$paradise["continent"]?></td>
		<td><?=$paradise["language"]?></td>
		<td><?=$paradise["religion"]?></td>
		<td><?=$paradise["population"]?></td>
		<td><?=$paradise["gdp"]?></td>
	</tr>
<?php endforeach ?>
</table>

<?php
include_once("include/footer.php");
?>
